package cn.gzmli.dtt.common.entity;

import cn.gzmli.dtt.sbzyc.validation.Add;
import cn.gzmli.dtt.sbzyc.validation.Delete;
import cn.gzmli.dtt.sbzyc.validation.Update;
import lombok.Data;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Set;

/**
 * @Descript
 * @Author xsx
 * @Date 2017/12/25
 */
@Entity
@Table(name = "role")
@Data
public class Role implements Serializable {

    private static final long serialVersionUID = -4977402012479272065L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @NotNull(groups = {Update.class,Delete.class})
    private Integer id;

    @NotNull(groups = {Update.class,Add.class})
    private String name;

    /*
    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name = "orgId", referencedColumnName = "id")
    private Department department;
    */

    @NotNull(groups = {Update.class,Add.class})
    private Integer type;

    @NotNull(groups = {Update.class,Add.class})
    private Integer status;

    @NotNull
    private String description;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(
            name = "role_menu",
            joinColumns = {@JoinColumn(name = "roleId", referencedColumnName = "id", table = "role")},
            inverseJoinColumns = {@JoinColumn(name = "menuId", referencedColumnName = "id", table = "menu")}
    )
    @OrderBy(value = "orderNo")
    private Set<Menu> menus;
    
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
            name = "role_permission",
            joinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "id", table = "role")},
            inverseJoinColumns = {@JoinColumn(name = "permission_id", referencedColumnName = "id", table = "permission")}
    )
    private Set<Permission> permissions;



	public String getPermissionIDS() {
        StringBuilder ids = new StringBuilder();
        if (this.permissions!=null){
            for (Permission permission : this.permissions){
                ids.append(permission.getId()).append(",");
            }
        }
        return ids.substring(0, ids.length()>1?ids.length()-1:0);
    }

	public String getMenuIDS() {
        StringBuilder ids = new StringBuilder();
        if (this.menus!=null){
            for (Menu menu : this.menus){
                ids.append(menu.getId()).append(",");
            }
        }
        return ids.substring(0, ids.length()>1?ids.length()-1:0);
    }

}
